using System;

namespace GridRegulator
{
    public class LineVariation
    {
        public static double[] ComputeHorizontal(GrayImage image, int x, int w)
        {
            double[] hv = new double[image.Height];

            for (int i = 0; i < image.Height; i++)
            {
                double v = 0;
                int d;
                for (int j = x + 1; j < x + w; j++)
                {
                    d = (image[i, j] - image[i, j-1]);
                    d *= d;
                    v += d;
                }
                v /= w-1;
                v = Math.Sqrt(v);

                hv[i] = v;
            }
            return hv;
        }

        public static double[] ComputeVertical(GrayImage image, int y, int h)
        {
            double[] vv = new double[image.Width];

            for (int i = 0; i < image.Width; i++)
            {
                double v = 0;
                int d;
                for (int j = y + 1; j < y + h; j++)
                {
                    d = (image[j, i] - image[j - 1, i]);
                    d *= d;
                    v += d;
                }
                v /= h - 1;
                v = Math.Sqrt(v);

                vv[i] = v;
            }
            return vv;
        }

        public static double[][] ComputeHorizontal(GrayImage image, int w)
        {
            int htbln = image.Width / w;
            double[][] htbl = new double[htbln][];

            for (int i = 0; i < htbln; i++)
            {
                htbl[i] = ComputeHorizontal(image, i * w, w);
            }

            return htbl;
        }

        public static double[][] ComputeVertical(GrayImage image, int h)
        {
            int vtbln = image.Height / h;
            double[][] vtbl = new double[vtbln][];

            for (int i = 0; i < vtbln; i++)
            {
                vtbl[i] = ComputeVertical(image, i * h, h);
            }

            return vtbl;
        }

    }   /* class LineVariation */
}   /* namespace GridRegulator */
